
import { ref } from 'vue'

const EachObject = (Obj, callback) => {
    for (const key in Obj) {
        if (Object.prototype.hasOwnProperty.call(Obj, key)) {
            const element = Obj[key];
            callback(element, key);
        }
    }
}

export class ExModel {

    /**
     * 参数表
     *
     * @type {Record<string, any>}
     * @memberof Model
     */
    ParamsMapping: Record<string, ExModelParam> = {}

    /**
     * 表单模型
     *
     * @type {any[]}
     * @memberof Model
     */
    FormView: any[] = []

    /**
     * 对象表格模型
     *
     * @type {any[]}
     * @memberof Model
     */
    TableView: any[] = []

    /**
     * 存储值
     *
     * @type {*}
     * @memberof Model
     */
    Data: any = ref({})

    /**
     * 地图关联元素
     *
     * @type {*}
     * @memberof Model
     */
    Entity: any = null

    /**
     * 模型名称
     *
     * @type {String}
     * @memberof Model
     */
    Title: string = ''

    /**
     * 模型类别
     *
     * @type {string}
     * @memberof Model
     */
    Type: string = ''

    /**
     * 模型基本配置
     *
     * @type {ExModelType}
     * @memberof Model
     */
    Config: ExModelType
    constructor(ModelConfig: ExModelType) {
        // 防止数据污染，进行深拷贝
        this.Config = JSON.parse(JSON.stringify(ModelConfig))
        this.Title = ModelConfig.Base.Title;
        this.Type = ModelConfig.Base.Type;
        
        const Value = {};
        // 格式化默认值
        ModelConfig.Param.forEach((element) => {
            this.ParamsMapping[element.Key] = element;
            if (element.hasOwnProperty('Default')) {
                Value[element.Key] = element.Default
            } else {
                switch (element.Type) {
                    case 'String':
                        Value[element.Key] = '';
                        break;
                    case 'Number':
                        Value[element.Key] = 0;
                        break;
                    case 'Int':
                        Value[element.Key] = 0;
                        break;
                    default:
                        Value[element.Key] = null;
                        break;
                }
            }
        });
        this.Data.value = Value;
    }
    GetDefaultFormView() {
        var FormConfig = this.Config.View.Form?.Default;
        if (!FormConfig) return [];
        var View = [];
        for (let i = 0; i < FormConfig.length; i++) {
            const element = FormConfig[i];
            const FormItem = {
                title: element.title,
                labelCol: 150,
                children: [],
            }
            for (let d = 0; d < element.children.length; d++) {
                const item = element.children[d];
                this.ParamsMapping[item] && FormItem.children.push(this.ParamsMapping[item]);
            };
            View.push(FormItem);
        }
        return View;
    }


}
